package com.lock.immersive.controller;

import com.alibaba.fastjson2.JSONArray;
import com.lock.immersive.annotation.AuthCheck;
import com.lock.immersive.exception.BusinessException;
import com.lock.immersive.mapper.MerchantMapper;
import com.lock.immersive.pojo.domain.Merchant;
import com.lock.immersive.pojo.dto.MerchantDto;
import com.lock.immersive.pojo.dto.MerchantRecommendDto;
import com.lock.immersive.service.MerchantService;
import com.lock.immersive.utils.ErrorCode;
import com.lock.immersive.utils.ResponseApi;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/merchant")
public class MerchantController {
    @Resource
    public MerchantService merchantService;

    @Resource
    public MerchantMapper merchantMapper;

    @PostMapping("/insertMerchant")
    @AuthCheck
    public ResponseApi<Object> insertMerchant(@RequestBody MerchantDto merchantDto, HttpServletRequest request) {
        return merchantService.insertMerchant(merchantDto, request);
    }

    @PostMapping("/updateRecommend")
    @AuthCheck(mustRole = "superManager")
    public ResponseApi<Object> updateRecommend(@RequestBody MerchantRecommendDto merchantRecommendDto, HttpServletRequest request) {
        return merchantService.updateRecommend(merchantRecommendDto, request);
    }

    @PostMapping("/updateMerchant")
    @AuthCheck(mustRole = "superManager")
    public ResponseApi<Object> updateMerchant(@RequestBody Merchant merchant, HttpServletRequest request) {
        return merchantService.updateMerchant(merchant, request);
    }

    @PostMapping("/deleteMerchantList")
    @AuthCheck
    public ResponseApi<Object> deleteMerchant(@RequestBody List<String> ids) {
        int i = merchantMapper.deleteBatchIds(ids);
        if (i > 0) {
            return ResponseApi.success("删除成功");
        } else {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
    }
}
